package com.dongbao.util;

import com.dongbao.core.config.MyState;
import com.dongbao.data.Datacenter;
import com.dongbao.entity.FileLog;
import com.dongbao.service.MyMqttClient;
import com.intellij.notification.NotificationType;

import java.net.*;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class InitUtil {

    public static void initFileList(){
        /* 初始化受保护的文件 */
        Datacenter.protectFiles.add(new FileLog("HolidayBalanceAsyncTask.java","李战","假期余额计算","假期余额计算", NotificationType.ERROR,"192.168.2.53"));
        Datacenter.protectFiles.add(new FileLog("WorkHourDayAsyncTask.java","曾鹏,刘弦","工时日报计算","工时日报计算",NotificationType.ERROR,"192.168.2.50,192.168.2.51"));
        Datacenter.protectFiles.add(new FileLog("WorkHourMonthAsyncTask.java","曾鹏","工时月报计算","工时月报计算",NotificationType.ERROR,"192.168.2.50,192.168.2.51"));
        Datacenter.protectFiles.add(new FileLog("GitUtil.java","王志坤","GIT工具","对git提交的内容进行分析",NotificationType.ERROR,"192.168.2.50,192.168.2.51,192.168.2.1,192.168.155.3,192.168.2.83"));
        Datacenter.protectFiles.add(new FileLog("MyApplicationService.kt","王志坤","IDEA全局服务插件","IDEA全局服务插件内容",NotificationType.ERROR,"192.168.2.50,192.168.2.51,192.168.155.3,192.168.2.83"));
        /* 链接MQ服务端 */
        connecteMQClient();
    }

    /**
     * 连接MQ
     * */
    public static void connecteMQClient(){
            /* 获取本机所有IP */
            IdeaApiUtil.showInfoNotification("开始获取当前主机所有ip地址",Datacenter.project);
            Map<String, List<String>> localIP = getLocalIP();
            /* 从获取的所有IP中 查找到指定IP段的IP地址 */
            for(String ipListKey:localIP.keySet()){
                if(localIP.get(ipListKey).size()>0){
                    List<String> ips = localIP.get(ipListKey);
                    /* 查找到指定IP段的IP地址 */
                    for (String ip : ips) {
                        if(ip.split("\\.")[2].equals(MyState.getInstance().getConfig().getIpScope())&&!ip.split("\\.")[3].equals("1")){
                            IdeaApiUtil.showInfoNotification("查找到指定IP段的地址",Datacenter.project);
                            Datacenter.clientIp = ip;
                        }
                        System.out.println("当前ip："+ip);
                    }
                }
            }
        subMessage();
    }

    public static void subMessage(){
        /* 订阅当前IP的消息 */
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Integer> future = executor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Datacenter.myMqttClient = new MyMqttClient();
                Datacenter.myMqttClient.subTopic(MyMqttClient.TOPIC_PREFIX+Datacenter.clientIp);
                /* 订阅群聊天面板消息 */
                Datacenter.myMqttClient.subTopic(MyMqttClient.TOPIC_PREFIX+MyMqttClient.TOPIC_ALL);
                return 3;
            }
        });
    }

    public static Map<String, List<String>> getLocalIP() {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        Enumeration<NetworkInterface> e1;
        try {
            e1 = NetworkInterface.getNetworkInterfaces();
            while (e1.hasMoreElements()) {
                NetworkInterface ni = e1.nextElement();

                List<String> ips = new LinkedList<String>();
                map.put(ni.getName(), ips);

                Enumeration<InetAddress> e2 = ni.getInetAddresses();
                while (e2.hasMoreElements()) {
                    InetAddress ia = e2.nextElement();
                    if(ia instanceof Inet6Address) {
                        continue; // omit IPv6 address
                    }
                    ips.add(ia.getHostAddress());
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return map;
    }

}
